package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * 视频列表数据
 * Created by zxh on 16/11/17.
 */

public class VideoListModel extends XListModel {
    public static final int LOAD_FREE_VIDEO = 0;
    public static final int LOAD_VIDEO_FROM_MENU = 1;

    private boolean loading;

    private int loadType;
    private MenuItem menuItem;

    public int getLoadType() {
        return loadType;
    }

    public void setLoadType(int loadType) {
        this.loadType = loadType;
        if (this.loadType == LOAD_FREE_VIDEO) {
            setCurrentPage(1);
            load(1);
        }
    }

    public MenuItem getMenuItem() {
        return menuItem;
    }

    public void setMenuItem(MenuItem menuItem) {
        this.menuItem = menuItem;
        if (this.loadType == LOAD_VIDEO_FROM_MENU) {
            setCurrentPage(1);
            load(1);
        }
    }

    public VideoListModel(Context context) {
        super(context);
        super.getAdapter().setVariableId(BR.video);
    }

    @Override
    public void load(final int page) {
        if (loading) {
            return;
        }
        loading = true;
        setNoMore();
        final Context context = getContext();
        final XListAdapterInterface adapter = super.getAdapter();
        adapter.clear(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                List videoList = null;
                switch (loadType) {
                    case LOAD_FREE_VIDEO:
                        videoList = WebApi.getFreeVideos(context);
                        getHandler().setTitle("免费试听");
                        break;
                    case LOAD_VIDEO_FROM_MENU:
                        if (menuItem == null) {
                            showToast("选定的菜单项无效");
                            return;
                        }
                        getHandler().setTitle(menuItem.getTitle());
                        videoList = WebApi.getVideoListByMenu(context, menuItem);
                        break;
                }

                try {
                    for (Object menu : videoList) {
                        LinkedTreeMap map = (LinkedTreeMap) menu;
                        VideoItem item = new VideoItem(context, R.layout.i_study);

                        item.setUid(Utils.getIntValue(map.get("uid")));
                        item.setCover((String) map.get("cover"));
                        item.setFreeflag(Integer.valueOf((String) map.get("freeflag")));
                        item.setNotepath((String) map.get("notepath"));
                        item.setPrice(Double.valueOf((String) map.get("price")));
                        item.setSpeaker((String) map.get("speaker"));
                        item.setUploadate(Utils.getDateFromTimestamp(map.get("uploaddate")));
                        item.setVideodesc((String) map.get("videodesc"));
                        item.setVideoid(Integer.valueOf((String) map.get("videoid")));
                        item.setVideoname((String) map.get("videoname"));
                        item.setVideopath((String) map.get("videopath"));
                        item.setVideotype((String) map.get("videotype"));
                        adapter.add(item);
                    }
                    if (videoList.size() > 0) {
                        setCurrentPage(page);
                    }
                    notifyDataSetChanged(videoList);
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                loading = false;
            }
        }).start();
    }
}
